/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.model.geo;

import java.awt.Color;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Point2D;
import java.io.Serializable;

public class GCubicCurve extends CubicCurve2D.Double implements Serializable {

	private static final long serialVersionUID = 1L;
	private GPoint p1;
	private GPoint ctrl1;
	private GPoint ctrl2;
	private GPoint p2;
	private int label = 0;
	private int nParts = 1;
	private Color color = Color.black;
	private GPoint[] discrete;
	private boolean isFrame = true;
	
//*****************************************************************************
	
	public GCubicCurve() {
		this(0, 0, 0, 0, 0, 0, 0, 0);
	}
	
//*****************************************************************************
	
	public GCubicCurve(double x1, double y1, double cx1, double cy1, double cx2, double cy2, double x2, double y2) {
		this(new GPoint(x1, y1), new GPoint(cx1, cy1), new GPoint(cx2, cy2), new GPoint(x2, y2));
	}
	
//*****************************************************************************
	
	public GCubicCurve(GPoint p1, GPoint c1, GPoint c2, GPoint p2) {
		this.p1 = p1;
		this.ctrl1 = c1;
		this.ctrl2 = c2;
		this.p2 = p2;
		this.discrete();
	}
	
//*****************************************************************************
	
	public Point2D getP1() {
		return p1;
	}
	
//*****************************************************************************
	
	public Point2D getP2() {
		return p2;
	}
	
//*****************************************************************************
	
	public Point2D getCtrl1() {
		return ctrl1;
	}
	
//*****************************************************************************
	
	public Point2D getCtrl2() {
		return ctrl2;
	}
	
//*****************************************************************************
	
	public double getX1() {
		return p1.getX();
	}
	
//*****************************************************************************
	
	public double getY1() {
		return p1.getY();
	}
	
//*****************************************************************************
	
	public double getX2() {
		return p2.getX();
	}
	
//*****************************************************************************
	
	public double getY2() {
		return p2.getY();
	}
	
//*****************************************************************************
	
	public double getXCtrl1() {
		return ctrl1.getX();
	}
	
//*****************************************************************************
	
	public double getYCtrl1() {
		return ctrl1.getY();
	}
	
//*****************************************************************************
	
	public double getXCtrl2() {
		return ctrl2.getX();
	}
	
//*****************************************************************************
	
	public double getYCtrl2() {
		return ctrl2.getY();
	}
	
//*****************************************************************************
	
	public boolean equals(Object obj) {
		
		if (obj instanceof CubicCurve2D) {
			CubicCurve2D li = (CubicCurve2D)obj;
			if ( (li.getP1().equals(this.getP1())) && (li.getP2().equals(this.getP2())) && (li.getCtrlP1().equals(this.getCtrlP1())) && (li.getCtrlP2().equals(this.getCtrlP2())) ) {
				return true;
			}
		}
		
		return false;
	}
	
//*****************************************************************************
	
	public void setLabel(int lb) {
		this.label = lb;
	}
	
//*****************************************************************************
	
	public int getLabel( ) {
		return label;
	}
	
//*****************************************************************************
	
	public void setNParts(int np) {
		this.nParts = np;
		this.discrete();
	}
	
//*****************************************************************************
	
	public int getNParts( ) {
		return nParts;
	}
	
//*****************************************************************************
	
	public void setDiscrete(GPoint[] discret) {
		this.discrete = (GPoint[])discret.clone();
	}
	
//*****************************************************************************
	
	private void discrete( ) {
		double nx, ny, t;
		double p1x, p1y, p2x, p2y, ctrl1x, ctrl1y, ctrl2x, ctrl2y;
		p1x = getX1();
		p1y = getY1();
		p2x = getX2();
		p2y = getY2();
		ctrl1x = getXCtrl1();
		ctrl1y = getYCtrl1();
		ctrl2x = getXCtrl2();
		ctrl2y = getYCtrl2();
		discrete = new GPoint[nParts+1];
		for (int count = 0; count <= nParts; count++) {
			t = (double)count/nParts;
			nx = -p1x*Math.pow(t,3)+3*p1x*Math.pow(t,2)+p1x+p2x*Math.pow(t,3)+3*Math.pow(t,3)*ctrl1x-6*Math.pow(t,2)*ctrl1x-3*t*p1x+3*t*ctrl1x-3*Math.pow(t,3)*ctrl2x+3*Math.pow(t,2)*ctrl2x;
			ny = -p1y*Math.pow(t,3)+3*p1y*Math.pow(t,2)+p1y+p2y*Math.pow(t,3)+3*Math.pow(t,3)*ctrl1y-6*Math.pow(t,2)*ctrl1y-3*t*p1y+3*t*ctrl1y-3*Math.pow(t,3)*ctrl2y+3*Math.pow(t,2)*ctrl2y;
			discrete[count] = new GPoint(nx,ny);
		}
	}
	
//*****************************************************************************
	
	public GPoint[] getDiscrete( ) {
		return discrete;
	}
	
//*****************************************************************************
	
	public void setColor(Color c) {
		this.color = c;
	}
	
//*****************************************************************************
	
	public Color getColor( ) {
		return color;
	}
	
//*****************************************************************************
	
	public double getAngle( ) {
		double angle = Math.atan2( (getY2()-getCtrlY2()), (getX2()-getCtrlX2()) );
		return angle;
	}
	
//*****************************************************************************
	
	public void setIsFrame(boolean a){
		isFrame = a;
	}
	
//*****************************************************************************
	
	public boolean isFrame(){
		return isFrame;
	}
	
//*****************************************************************************
}
